package com.brayan.webapp.controller.realestate;

import java.io.Serializable;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;

import com.brayan.webapp.model.realestate.Reservation;
import com.brayan.webapp.model.realestate.ReservationState;
import com.brayan.webapp.repository.realestate.ReservationRepository;
import com.brayan.webapp.service.realestate.ReservationBean;

@ManagedBean
@SessionScoped
public class ReservationStatusController implements Serializable {
	
	private static final long serialVersionUID = 3003659781021228584L;

	@Inject
	transient private Logger LOGGER;
	
	@Inject
	@Named("facesContext")
	transient private FacesContext facesContext;

	@Inject
	ReservationRepository reservationRepo;
	@Inject
	private ReservationBean reservationService;

	private Long reservationId; 
	private Reservation reservation;
	private String newState;
	private String oldState;

	public Long getReservationId() {
		return reservationId;
	}

	public void setReservationId(Long reservationId) {
		this.reservationId = reservationId;
	}
	
	public Reservation getReservation() {
		return reservation;
	}

	public void setReservation(Reservation reservation) {
		this.reservation = reservation;
	}

	public String updateReservationState(String newState) {
		ReservationState newResState = null;
		try {
			newResState = ReservationState.valueOf(newState);
		} catch (Exception ex) {
			LOGGER.error("Failed to update a reservation, because the new state string (" + newState
					+ ") is not an official state.", ex);
			return "reservationdetails";
		}
		lookupReservation();
		if(newState.equalsIgnoreCase(ReservationState.REJECTED.toString())){
			//facesContext.addMessage(null, new FacesMessage("The reservation has been deleted and the visitor informed."));
			reservationService.deleteReservation(getReservation());
			return "reservations";
		}
		else{
			reservationService.updateReservationState(getReservation(), newResState);
			return "reservationdetails";
		}
	}
	
	public void lookupReservation(){
		setReservation(reservationRepo.getReservationsById(reservationId));
	}

	private Long getParamIdFromUrl() {
		FacesContext context = FacesContext.getCurrentInstance();
		ExternalContext extContext = context.getExternalContext();
		Map<String, String> params = extContext.getRequestParameterMap();
		int id = Integer.parseInt(params.get("id"));
		return (long) id;
	}
	
	public boolean isNew() {
		if (reservation == null)
			return false;
		else
			return (reservation.getReservationState() == ReservationState.NEW);
	}

	public boolean isAccepted() {
		if (reservation == null)
			return false;
		else
			return (reservation.getReservationState() == ReservationState.ACCEPTED);
	}

	public boolean isConfirmed() {
		if (reservation == null)
			return false;
		else
			return (reservation.getReservationState() == ReservationState.CONFIRMED);
	}

	public boolean isSettled() {
		if (reservation == null)
			return false;
		else
			return (reservation.getReservationState() == ReservationState.SETTLED);
	}

	public boolean isRejected() {
		if (reservation == null)
			return false;
		else
			return (reservation.getReservationState() == ReservationState.REJECTED);
	}

	public String getNewState() {
		return newState;
	}

	public String getOldState() {
		return oldState;
	}

	public void setOldState(String oldState) {
		this.oldState = oldState;
	}

}
